package com.virgil.core.config;

import java.lang.reflect.Method;

import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.servlet.mvc.condition.RequestCondition;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import com.virgil.core.annotation.Version;

/**
 * 
 * @author Virgil
 * @Date 2020年8月31日
 * @Describe
 *    <p>
 *		自定义匹配处理器
 *     </p>
 */
public class CustomRequestMappingHandlerMapping extends RequestMappingHandlerMapping{


   @Override
   protected RequestCondition<?> getCustomTypeCondition(Class<?> handlerType) {
       Version version = AnnotationUtils.findAnnotation(handlerType, Version.class);
       return createCondition(version);
   }

   @Override
   protected RequestCondition<?> getCustomMethodCondition(Method method) {
	   Version version = AnnotationUtils.findAnnotation(method, Version.class);
       return createCondition(version);
   }

   private RequestCondition<ApiVersionCondition> createCondition(Version version) {
       return version == null ? null : new ApiVersionCondition(version.value());
   }

}